﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace ComicSharp.Helpers
{
    public static class ImageHelper
    {
        public enum RotateDegrees
        {
            Zero = 0,
            Ninety = 90,
            OneEighty = 180,
            TwoSeventy = 270
        }

        /// <summary>
        /// Rotates the and scale image.
        /// </summary>
        /// <param name="file">The file.</param>
        /// <param name="degrees">The degrees (clockwise).</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <returns>A rotated and scale bitmap object</returns>
        public static Bitmap RotateAndScaleImage(string file, RotateDegrees degrees, int width, int height)
        {
            // Load the image from the path
            Image img = Image.FromFile(file);

            // Rotate the image clockwise based on the degrees
            switch (degrees)
            {
                case RotateDegrees.Zero:
                    break;

                case RotateDegrees.Ninety:
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                    break;

                case RotateDegrees.OneEighty:
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone);
                    break;

                case RotateDegrees.TwoSeventy:
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone);
                    break;

                default:
                    break;
            }           

            // Convert the image to a bit map
            Bitmap returnBitmap = new Bitmap(img);

            // Clean up resources
            img.Dispose();

            // Scale image to screen size and return
            return ProportionallyResizeBitmap(returnBitmap, width, height);
        }

        /// <summary>
        /// Proportionallies the resize bitmap.
        /// </summary>
        /// <param name="bmap">The bmap.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <returns>A resized bitmap based on the input image</returns>
        private static Bitmap ProportionallyResizeBitmap(Bitmap bmap, int width, int height)
        {
            // Save the original dimensions
            int oldWidth = bmap.Width;
            int oldHeight = bmap.Height;

            // Determine the longest and shortest dimension
            int longestDimension = (oldWidth > oldHeight) ? oldWidth : oldHeight;
            int shortestDimension = (oldWidth < oldHeight) ? oldWidth : oldHeight;

            // Determine the proportionality
            float factor = ((float)longestDimension) / shortestDimension;

            // Set the default new width and height
            double newWidth = width;
            double newHeight = height;

            // Recalc if height is greater than width
            if (oldWidth < oldHeight)
            {
                newWidth = height / factor;
                newHeight = height;
            }

            // Create a new bitmap object with the calculated proportional dimensions
            Bitmap result = new Bitmap((int)newWidth, (int)newHeight);

            // Convert the passed in bitmap object to the new size
            using (Graphics graph = Graphics.FromImage((System.Drawing.Image)result))
            {
                graph.DrawImage(bmap, 0, 0, (int)newWidth, (int)newHeight);
            }

            return result;
        }
    }
}
